package com.lucy.disk.converter;

import com.lucy.common.utils.JsonUtil;
import com.lucy.disk.constant.FileTypeEnum;
import com.lucy.disk.domain.model.BaseFile;
import com.lucy.disk.domain.model.ImageModel;
import com.lucy.disk.domain.model.VideoModel;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
 * @author kindear
 */
@Deprecated
@Converter
public class FileExtendConverter implements AttributeConverter<BaseFile,String> {
    @Override
    public String convertToDatabaseColumn(BaseFile baseFile) {
        return JsonUtil.obj2String(baseFile);
    }

    @Override
    public BaseFile convertToEntityAttribute(String s) {
        BaseFile baseFile = JsonUtil.string2Obj(s,BaseFile.class);
        assert baseFile != null;
        if (baseFile.getFileType().equals(FileTypeEnum.PICTURE.getDesc())){
            return JsonUtil.string2Obj(s, ImageModel.class);
        }else if (baseFile.getFileType().equals(FileTypeEnum.VIDEO.getDesc())){
            return JsonUtil.string2Obj(s, VideoModel.class);
        }else {
            return baseFile;
        }
    }
}
